"""
鸢尾花类别预测
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.datasets as sd
import sklearn.model_selection as ms
import sklearn.linear_model as lm
import sklearn.metrics as sm
import sklearn.tree as st

iris = sd.load_iris()
# print(data.keys())
# print(data.DESCR)

data = pd.DataFrame(iris.data,columns=iris.feature_names)
data['target'] = iris.target
# print(data['target'].values)

print(data.columns)
# 萼片可视化
data.plot.scatter(x='sepal length (cm)',y='sepal width (cm)',c='target',cmap='brg')
# 花瓣可视化
data.plot.scatter(x='petal length (cm)',y='petal width (cm)',c='target',cmap='brg')
# plt.show()

# 1.拿到1类别和2类别的数据
# mask = (data['target'] == 1) | (data['target'] == 2)
# sub_data = data[mask]
# print(sub_data.target.value_counts())
# 2.整理输入集和输出集
x = data.iloc[:,:-1]
y = data.iloc[:,-1]
# 划分训练集和测试集 stratify=y：按照类别均衡划分
train_x,test_x,train_y,test_y = ms.train_test_split(x,y,test_size=0.2,random_state=7,stratify=y)
# 逻辑回归分类模型
# model = lm.LogisticRegression(solver='liblinear')
model = st.DecisionTreeClassifier(max_depth=4)

# 进行5次交叉验证
scores = ms.cross_val_score(model,x,y,cv=5,scoring='f1_weighted')

print(scores.mean())

model.fit(train_x,train_y)

pred_test_y = model.predict(test_x)

# 准确率：对的个数 / 总个数
# print(test_y.values)
# print(pred_test_y)
# print((test_y == pred_test_y).sum() / test_y.size)
# 调用接口计算准确率
print('准确率：',sm.accuracy_score(test_y,pred_test_y))
# macro：计算平均值，不考虑样本权重
print('召回率：',sm.recall_score(test_y,pred_test_y,average='macro'))
print('查准率：',sm.precision_score(test_y,pred_test_y,average='macro'))
print('f1:',sm.f1_score(test_y,pred_test_y,average='macro'))
print('混淆矩阵：\n',sm.confusion_matrix(test_y,pred_test_y))
print('分类报告：\n',sm.classification_report(test_y,pred_test_y))